package cn.billy.serve.service.impl;

import cn.billy.basic.utils.BaiduAuditUtils;
import cn.billy.basic.utils.LoginContext;
import cn.billy.pet.domain.Pet;
import cn.billy.pet.domain.PetDetail;
import cn.billy.pet.domain.PetOnlineAuditLog;
import cn.billy.serve.domain.Product;
import cn.billy.serve.domain.ProductDetail;
import cn.billy.serve.domain.ProductOnlineAuditLog;
import cn.billy.serve.mapper.ProductDetailMapper;
import cn.billy.serve.mapper.ProductMapper;
import cn.billy.serve.mapper.ProductOnlineAuditLogMapper;
import cn.billy.serve.service.IProductService;
import cn.billy.basic.service.impl.BaseServiceImpl;
import org.apache.velocity.runtime.directive.Foreach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author billy
 * @since 2022-07-21
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private ProductOnlineAuditLogMapper onlineAuditLogMapper;

    @Override
    public void save(Product product) {
        productMapper.add(product);
        ProductDetail detail = product.getDetail();
        detail.setProductId(product.getId());
        productDetailMapper.add(detail);
    }

    @Override
    public void onSalePet(List<Long> ids) {
        // 遍历Ids
        for (Long id : ids) {
            // 1.得到每个宠物基本信息对象
            Product product = productMapper.getById(id);
            StringBuffer sbf = new StringBuffer();
            // 2.调用百度AI进行文字，图片的审核
            if (!BaiduAuditUtils.TextCensor(product.getName())){
                sbf.append("服务名称不合法，宠物名称为：" + product.getName());
            }
            /*if (!BaiduAuditUtils.ImgCensor("123.207.27.208" + pet.getResources().split(",")[0])){
                sbf.append("宠物图片不合法，宠物图片路径：" + pet.getResources().split(",")[0]);
            }*/
            if (sbf.length() > 0){
                // 2.1.如果违规，我们要保存审核失败日志
                ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
                productOnlineAuditLog.setProductId(id);
                productOnlineAuditLog.setNote(sbf.toString());
                onlineAuditLogMapper.add(productOnlineAuditLog);
                continue;
            }
            productMapper.onsale(id);
        }

    }

    @Override
    public void offSalePet(List<Long> ids) {
        productMapper.offsale(ids);
    }

    @Override
    public void batchDel(Long[] ids) {
        for (Long id: ids){
            ProductDetail byId = productDetailMapper.getById(id);
            if (byId != null){
                productDetailMapper.remove(byId.getId());
            }
        }
        productMapper.batchDel(ids);
    }

    @Override
    public void update(Product product) {
        ProductDetail detail = product.getDetail();
        ProductDetail productDetail = productDetailMapper.getProductId(product.getId());
        detail.setId(productDetail.getId());
        detail.setProductId(product.getId());
        productDetailMapper.update(detail);
//        detail1.setIntro(detail.getIntro());
//        detail1.setOrderNotice(detail.getOrderNotice());
//        productDetailMapper.update(detail1);
        productMapper.update(product);
    }
}
